#!/bin/bash

source /home/shell/util.sh

yum install -y zlib* mysql-devel libffi-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel libpcap xz-devel openssl

printInfo "Python $(date +%Y)年版本："
curl -sL http://npm.taobao.org/mirrors/python/|grep $(date +%Y)|grep -v test|grep -v doc|perl -pe "s/.*>(.*)\/<.*/\1/g"
while [ true ]; do
    read -p "选择Python版本：" version
    if [[ -z "${version}" ]]; then
        printFail "请重新输入版本！"
        continue;
    else
        break;
    fi
done

cd /opt
if [ ! -f "Python-${version}.tgz" ];then
 # wget --content-disposition --no-check-certificate https://www.python.org/ftp/python/${version}/Python-${version}.tgz
 download /opt/Python.tgz http://npm.taobao.org/mirrors/python/${version}/Python-${version}.tgz
fi
tar -zxf Python.tgz -C /opt
mv Python-${version} python
cd python
./configure --prefix=/usr/local/python3 --enable-optimizations
make && make install

# 创建命令
ln -s /usr/local/python3/bin/python3 /usr/local/python3/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip

cp /home/shell/support/python.sh /etc/profile.d/
chmod 777 /etc/profile.d/python.sh
source /etc/profile.d/python.sh

if [ ! -f "~/.pip/pip.conf" ];then
  mkdir -p ~/.pip/
  cp -r /home/shell/support/pip.conf ~/.pip/pip.conf
fi

python -m pip install --upgrade pip
# 信息显示
printInfo "==============================python=============================="
python -V
pip -V
printInfo "==============================python=============================="

# 清理
rm -rf /opt/Python-${version}.tgz